﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.Controller;
using whatpricemyhouse.LINQ;
using System.Web.Security;

namespace whatpricemyhouse.agent
{
    public partial class UpdateYourDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new AdminBasicPage();
            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    TextBox firstName = this.LoginView2.FindControl("firstNameTxt") as TextBox;
                    if (firstName != null)
                    {
                        loadProfile();
                        ImageButton ImageButton1 = this.LoginView2.FindControl("ImageButton1") as ImageButton;

                        ImageButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
                        ImageButton1.Attributes.Add("onmouseout", "mouseOut1(this);");
                    }

                }
            }
        }

        private void loadProfile()
        {
            TextBox firstName = this.LoginView2.FindControl("firstNameTxt") as TextBox;
            TextBox sureNameTxt = this.LoginView2.FindControl("sureNameTxt") as TextBox;
            TextBox businessNameTxt = this.LoginView2.FindControl("businessNameTxt") as TextBox;
            TextBox addressNameTxt = this.LoginView2.FindControl("addressNameTxt") as TextBox;
            TextBox suburbTxt = this.LoginView2.FindControl("suburbTxt") as TextBox;
            RegionSelector regionSelector = this.LoginView2.FindControl("regionSelector") as RegionSelector;
            PlaceSelector placeSelector1 = this.LoginView2.FindControl("placeSelector1") as PlaceSelector;
            PlaceSelector placeSelector2 = this.LoginView2.FindControl("placeSelector2") as PlaceSelector;
            PlaceSelector placeSelector3 = this.LoginView2.FindControl("placeSelector3") as PlaceSelector;
            DropDownList countryDDL = this.LoginView2.FindControl("countryDDL") as DropDownList;
            TextBox moblieTxt = this.LoginView2.FindControl("moblieTxt") as TextBox;
            TextBox phoneTxt = this.LoginView2.FindControl("phoneTxt") as TextBox;
            TextBox faxTxt = this.LoginView2.FindControl("faxTxt") as TextBox;
            TextBox emailTxt = this.LoginView2.FindControl("emailTxt") as TextBox;
            TextBox websiteTxt = this.LoginView2.FindControl("websiteTxt") as TextBox;
            DropDownList autoReportDDL = this.LoginView2.FindControl("autoReportDDL") as DropDownList;
            DropDownList autoSummaryDDL = this.LoginView2.FindControl("autoSummaryDDL") as DropDownList;


            DataControllerDataContext dcdc = new DataControllerDataContext();
            var users = from u in dcdc.aspnet_Users
                        where u.UserName == User.Identity.Name
                        select u;
            Guid agentId = new Guid();
            foreach (var user in users)
            {
                agentId = user.UserId;
                break;
            }


            var agents = from u in dcdc.agent_info_tables
                         where u.UserID == agentId
                         select u;
            foreach (var agent in agents)
            {
                firstName.Text = agent.FirstName;
                sureNameTxt.Text = agent.LastName;
                businessNameTxt.Text = agent.buinessName;
                addressNameTxt.Text = agent.address;
                suburbTxt.Text = agent.suburb;
                var placeIDs = from p in dcdc.place_agent_tables
                               where p.UserID == agentId
                               select p;
                int i = 0;
                foreach (var placeId in placeIDs)
                {
                    i++;
                    if (i == 1)
                    {
                        placeSelector1.setSelectedValue(placeId.suburb.suburbid.ToString());
                    }
                    if (i == 2)
                    {
                        placeSelector2.setSelectedValue(placeId.suburb.suburbid.ToString());
                    }
                    if (i == 3)
                    {
                        placeSelector3.setSelectedValue(placeId.suburb.suburbid.ToString());
                    }

                    regionSelector.setSelectedValue(placeId.suburb.area);
                }

                countryDDL.SelectedValue = agent.country;
                moblieTxt.Text = agent.Mobile;
                phoneTxt.Text = agent.Phone;
                faxTxt.Text = agent.Fax;
                //emailTxt.Text
                MembershipUser u1 = Membership.GetUser(User.Identity.Name);
                emailTxt.Text = u1.Email;
                websiteTxt.Text = agent.web;
                autoReportDDL.SelectedValue = agent.autoReportValue;
                autoSummaryDDL.SelectedValue = agent.autoSummaryValue;

            }
        }



        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            TextBox firstName = this.LoginView2.FindControl("firstNameTxt") as TextBox;
            TextBox sureNameTxt = this.LoginView2.FindControl("sureNameTxt") as TextBox;
            TextBox businessNameTxt = this.LoginView2.FindControl("businessNameTxt") as TextBox;
            TextBox addressNameTxt = this.LoginView2.FindControl("addressNameTxt") as TextBox;
            TextBox suburbTxt = this.LoginView2.FindControl("suburbTxt") as TextBox;
            PlaceSelector placeSelector1 = this.LoginView2.FindControl("placeSelector1") as PlaceSelector;
            PlaceSelector placeSelector2 = this.LoginView2.FindControl("placeSelector2") as PlaceSelector;
            PlaceSelector placeSelector3 = this.LoginView2.FindControl("placeSelector3") as PlaceSelector;
            DropDownList countryDDL = this.LoginView2.FindControl("countryDDL") as DropDownList;
            TextBox moblieTxt = this.LoginView2.FindControl("moblieTxt") as TextBox;
            TextBox phoneTxt = this.LoginView2.FindControl("phoneTxt") as TextBox;
            TextBox faxTxt = this.LoginView2.FindControl("faxTxt") as TextBox;
            TextBox emailTxt = this.LoginView2.FindControl("emailTxt") as TextBox;
            TextBox websiteTxt = this.LoginView2.FindControl("websiteTxt") as TextBox;
            DropDownList autoReportDDL = this.LoginView2.FindControl("autoReportDDL") as DropDownList;
            DropDownList autoSummaryDDL = this.LoginView2.FindControl("autoSummaryDDL") as DropDownList;


            DataControllerDataContext dcdc = new DataControllerDataContext();
            var users = from u in dcdc.aspnet_Users
                        where u.UserName == User.Identity.Name
                        select u;

            Guid agentId = new Guid();

            foreach (var user in users)
            {
                agentId = user.UserId;
                break;
            }


            var agents = from u in dcdc.agent_info_tables
                         where u.UserID == agentId
                         select u;
            foreach (var agent in agents)
            {
                agent.FirstName = firstName.Text;
                agent.LastName = sureNameTxt.Text;
                agent.buinessName = businessNameTxt.Text;
                agent.address = addressNameTxt.Text;
                agent.suburb = addressNameTxt.Text;
                //placeSelector1
                //placeSelector2
                //placeSelector3
                var places = from p in dcdc.place_agent_tables
                             where p.UserID == agentId
                             select p;
                int placeCount = 0;
                foreach (var place in places)
                {
                    placeCount++;
                    if (placeCount == 1)
                    {
                        var _p = (from _pp in dcdc.place_agent_tables where _pp.ID == place.ID select _pp).Single();
                        _p.suburb = dcdc.suburbs.Single(_pp => _pp.suburbid == placeSelector1.getSelectedValue());
                    }
                    if (placeCount == 2)
                    {
                        var _p = (from _pp in dcdc.place_agent_tables where _pp.ID == place.ID select _pp).Single();
                        _p.suburb = dcdc.suburbs.Single(_pp => _pp.suburbid == placeSelector2.getSelectedValue());
                    }
                    if (placeCount == 3)
                    {
                        var _p = (from _pp in dcdc.place_agent_tables where _pp.ID == place.ID select _pp).Single();
                        _p.suburb = dcdc.suburbs.Single(_pp => _pp.suburbid == placeSelector3.getSelectedValue());
                    }
                }

                switch (placeCount)
                {
                    case 0:
                        place_agent_table pat1 = new place_agent_table();
                        pat1.suburbid = placeSelector1.getSelectedValue();
                        pat1.UserID = agentId;
                        dcdc.place_agent_tables.InsertOnSubmit(pat1);

                        place_agent_table pat2 = new place_agent_table();
                        pat2.suburbid = placeSelector2.getSelectedValue();
                        pat2.UserID = agentId;
                        dcdc.place_agent_tables.InsertOnSubmit(pat2);

                        place_agent_table pat3 = new place_agent_table();
                        pat3.suburbid = placeSelector3.getSelectedValue();
                        pat3.UserID = agentId;
                        dcdc.place_agent_tables.InsertOnSubmit(pat3);
                        break;
                    case 1:
                        place_agent_table pat4 = new place_agent_table();
                        pat4.suburbid = placeSelector2.getSelectedValue();
                        pat4.UserID = agentId;
                        dcdc.place_agent_tables.InsertOnSubmit(pat4);

                        place_agent_table pat5 = new place_agent_table();
                        pat5.suburbid = placeSelector3.getSelectedValue();
                        pat5.UserID = agentId;
                        dcdc.place_agent_tables.InsertOnSubmit(pat5);
                        break;
                    case 2:
                        place_agent_table pat6 = new place_agent_table();
                        pat6.suburbid = placeSelector3.getSelectedValue();
                        pat6.UserID = agentId;
                        dcdc.place_agent_tables.InsertOnSubmit(pat6);
                        break;
                }

                agent.country = countryDDL.SelectedValue;
                agent.Mobile = moblieTxt.Text;
                agent.Phone = phoneTxt.Text;
                agent.Fax = faxTxt.Text;
                MembershipUser u1 = Membership.GetUser(User.Identity.Name);
                if (!String.IsNullOrEmpty(emailTxt.Text))
                {
                    u1.Email = emailTxt.Text;
                    Membership.UpdateUser(u1);
                }
                agent.web = websiteTxt.Text;
                agent.autoReportValue = autoReportDDL.SelectedValue;
                agent.autoSummaryValue = autoSummaryDDL.SelectedValue;

                dcdc.SubmitChanges();
                break;
            }

        }
    }
}